package info.pinlab.pinplayer.control;

import info.pinlab.pinplayer.model.response.ResponseContent;
import info.pinlab.pinplayer.model.response.ResponseContentAudio;
import info.pinlab.pinplayer.model.response.ResponseContentEmpty;
import info.pinlab.pinplayer.model.rule.AudioRule;
import info.pinlab.pinplayer.view.RecordTaskView;
import info.pinlab.pinplayer.view.TaskView;
import info.pinlab.pinsound.WavClip;
import info.pinlab.pinsound.app.AudioPlayer;
import info.pinlab.pinsound.app.AudioPlayerListener;
import info.pinlab.pinsound.app.AudioPlayerView;
import info.pinlab.pinsound.app.AudioRecorder;
import info.pinlab.pinsound.app.AudioRecorderListener;
import info.pinlab.pinsound.app.AudioRecorderView;


public class RecordTaskController extends AbstractTaskController
implements AudioRecorderListener, AudioPlayerListener, AudioController{
	
	private AudioRule arule = null;
	private AudioRecorder recorder = null;
	private AudioPlayer player = null;
	
	private long recStartT = -1;
	private long recStopT = -1;
	
	private RecordTaskView view = null;
	
	public RecordTaskController(){
	}
	
	
	@Override
	public void reqNextByUsr(){
//		System.out.println(" REC " + recorder.isRecording() + " " + recorder);
//		System.out.println(" PLAY " + player.isPlaying() + " " + player);
		if(player.isPlaying()){
			//-- can't step when playing --//
			logger.debug("Can't step when playing");
			return;
		}
		if(recorder.isRecording()){
			//-- can't step when recording --//
			logger.debug("Can't step when recording");
			return;
		}
		super.reqNextByUsr();
	}
	
	
	@Override
	public void reqPrevByUsr(){
		if(player.isPlaying()){
			//-- can't step when playing --//
			return;
		}
		if(recorder.isRecording()){
			//-- can't step when recording --//
			return;
		}
		super.reqPrevByUsr();
	}
	

	
	@Override
	public void setView(TaskView view) {
		super.setView(view);
		this.view = (RecordTaskView) view;
		if(view instanceof AudioRecorderView){
			if(recorder != null){
				recorder.setAudioRecorderView((AudioRecorderView)this.view);
			}
		}
		if(view instanceof AudioPlayerView){
			player.setAudioPlayerView((AudioPlayerView) this.view);
		}
	}
	

	public void setAudioRule(AudioRule arule){
		this.arule = arule;
	}


	public ResponseContent getRecording(){
		if(recorder!=null){
			WavClip wav = recorder.getWavClip();
			if(wav==null){
				logger.warn("No wav available from recorder, but recorder is set! " + this);
				return new ResponseContentEmpty(recStopT, (int)(recStopT - recStartT));
			}else{
				ResponseContentAudio respAudio = new ResponseContentAudio(recStopT, 
						(int)(recStopT - recStartT), wav);
				return respAudio;
			}
		}else{
			logger.error("No audio recorder is set!");
			return new ResponseContentEmpty(recStopT, (int)(recStopT - recStartT));
		}
	}
	
	
	
	int recN = 0;
	
	@Override
	public void reqRecStart(){
//		System.out.println("A RULE " + arule);
		if(arule != null){
			if(!arule.canRec){
				logger.warn("User can't record on this task!");
				return;
			}
			if(arule.maxRecN > recN){
				logger.warn("Too many recording tries! " + recN + "/" + arule.maxRecN);
				return;
			}
		}
		
		recStartT = System.currentTimeMillis();
//		System.out.println("REQ record: is rec " + recorder.isRecording() + " is play "+ player.isPlaying());
		if (recorder!= null && !recorder.isRecording()){
//			if(player.isPlaying()){
//				//-- don't record if playing --//
//				return;
//			}
			
			recorder.reqRecStart();
			recN++;
		}
	}

	@Override
	public void reqRecStop() {
		recStopT = System.currentTimeMillis();
		if(recorder!=null){
			recorder.reqRecStop();
		}
	}


	@Override
	public void reqPauseToggle() {
		player.reqPauseToggle();
	}


	@Override
	public void reqPlay(){
		player.reqPlay();
	}


	@Override
	public void reqPosInMs(long ms) {
		player.reqPosInMs(ms);
	}


	@Override
	public void reqStop() {
		player.reqStop();
	}

	@Override
	public void setAudioRecorder(AudioRecorder arecorder) {
		this.recorder = arecorder;
		recorder.setAfterRecHook(new Runnable() {
			@Override
			public void run() {
				WavClip wav = recorder.getWavClip();
				player.setAudio(wav);
			}
		});
	}

	@Override
	public void setAudioPlayer(AudioPlayer aplayer) {
		this.player = aplayer;
		player.setAfterPlayHook(new Runnable() {
			@Override
			public void run(){
				view.setReadyToRecState();
			}
		});
		
	}
	
}
